package com.hursing.tractor.baseview;

import com.hursing.tractor.model.Card;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public final class UIFlippedSuitView extends UIView {
	
	int mFlippedCount;
	Bitmap[] mSuitBitmaps;
	int mFlippedSuit = Card.SUIT_UNKNOWN;
	Rect mBitmapSrcRect;
	Rect mBitmapDstRect;
	int mInterval;
	
	@Override
	public UIFlippedSuitView toFlippedSuitView() { return this; }
	
	public UIFlippedSuitView flippedCount(int count) {
		mFlippedCount = count;
		return this;
	}
	
	public UIFlippedSuitView suitBitmaps(Bitmap[] bitmaps) {
		mSuitBitmaps = bitmaps;
		mBitmapSrcRect = new Rect(0, 0, mSuitBitmaps[0].getWidth(), mSuitBitmaps[0].getHeight());
		return this;
	}
	
	public UIFlippedSuitView flippedSuit(int suit) {
		mFlippedSuit = suit;
		return this;
	}
	
	@Override
	public UIView frame(Rect rect) {
		super.frame(rect);
		mBitmapDstRect = new Rect(rect);
		mInterval = mBitmapDstRect.height();
		mBitmapDstRect.right = mBitmapDstRect.left + mInterval;
		return this;
	}
	
	@Override
	protected void doDraw (Canvas canvas) {
		if (mFlippedSuit == Card.SUIT_UNKNOWN) {
			return;
		}
		Bitmap bitmap = mSuitBitmaps[mFlippedSuit];
		mBitmapDstRect.offsetTo(mFrame.left, mFrame.top);
		for (int i = 0; i < mFlippedCount; i++) {
			canvas.drawBitmap(bitmap, mBitmapSrcRect, mBitmapDstRect, null);
			mBitmapDstRect.offset(mInterval, 0);
		}
	}
	
	@Override
	public int measureWidth() {
		return mFrame.height() * 4;
	}
}
